package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;


import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class,timeout = 50000)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入DAO对象
    //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;


    public Result add(CheckItem checkItem) {
        //数据校验
        //判断数据是否合法


        try {
            checkItemDao.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //判断currentPage是否合法 》=1
        if (queryString != null) {
            PageHelper.startPage(1, pageSize);
        } else {
            PageHelper.startPage(currentPage, pageSize);
        }
        //完成分页查询 基于mybatis框架提供的分页助手插件完成
        //双向绑定

        Page<CheckItem> pageResults = (Page<CheckItem>) checkItemDao.selectByCondition(queryString);

        long total = pageResults.getTotal();
        System.out.println(total);
        List<CheckItem> row = pageResults.getResult();

        System.out.println(row);

        //PageInfo<CheckItem> pageInfo = new PageInfo<>();
        //pageInfo.setList(row);

        return new PageResult(total, row);
    }

    @Override
    public void deleteById(int id) {
        //当前检查项是否关联检查组
        long count = checkItemDao.findCountByCheckId(id);
        if (count > 0) {
            //当前检查项已经关联检查组了
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public Result edit(CheckItem checkItem) {
        //该有的校验不要少
        if (checkItem.getName() == null||checkItem.getName().length()==0) {
            return new Result(false, "项目名称不能为空");
        }
        if (checkItem.getCode() == null||checkItem.getCode().length()==0) {
            return new Result(false, "项目编码不能为空");
        }
        try {
            checkItemDao.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public Result findAll() {
        try{
            List<CheckItem> checkItems = checkItemDao.selectByCondition("");

            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


}
